<!--
 * @Descripttion: 熊大写的
 * @version: 1.0.0
 * @Author: xt_xiong
 * @Date: 2022-01-18 15:28:08
 * @LastEditors: xt_xiong
 * @LastEditTime: 2022-01-21 20:28:12
-->
<template>
  <div class="container">
    <el-card>
      <div v-if="$route.query.id">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item
            :to="{ path: '/subjects/list' }"
            id="subMangerDesign"
            >学科管理</el-breadcrumb-item
          >
          <el-breadcrumb-item>{{ $route.query.name }}</el-breadcrumb-item>
          <el-breadcrumb-item>目录管理</el-breadcrumb-item>
        </el-breadcrumb>
        <el-divider></el-divider>
      </div>
      <div class="left">
        <!-- 表头区域 -->
        <el-form inline ref="form" :model="form" label-width="80px">
          <el-form-item label="目录名称">
            <el-input v-model="directoryName"> </el-input>
          </el-form-item>
          <el-form-item label="状态">
            <!-- <el-input v-model="form.name"> -->
            <el-select v-model="state" placeholder="请选择">
              <el-option label="启用" value="1"> </el-option>
              <el-option label="禁用" value="0"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="clear">清除</el-button>
            <el-button type="primary" @click="querySub">搜索</el-button>
          </el-form-item>
          <el-form-item style="float: right">
            <el-link
              type="primary"
              :underline="false"
              class="el-button el-button--text el-button--medium"
              v-if="$route.query.id"
              @click="$router.back()"
            >
              <i class="el-icon-back"></i>
              返回学科
            </el-link>
            <el-button type="success" @click="dialogVisible = true"
              >新增目录</el-button
            >
          </el-form-item>
          <el-form-item style="display: block; background-color: #f4f4f5">
              <el-alert
              :closable="false"
              :title="`数据一共${this.total}条`"
              type="info"
              show-icon>
              </el-alert>
          </el-form-item>
        </el-form>
        <!-- 表单区域 -->
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" :index="indexMethods" label="序号">
          </el-table-column>
          <el-table-column
            width="120"
            sortable
            prop="subjectName"
            label="所属学科"
          >
          </el-table-column>
          <el-table-column
            width="120"
            sortable
            prop="directoryName"
            label="目录名称"
          >
          </el-table-column>
          <el-table-column sortable width="120" prop="username" label="创建者">
          </el-table-column>
          <el-table-column
            sortable
            prop="addDate"
            :formatter="dateFormat"
            width="180"
            label="创建日期"
          >
          </el-table-column>
          <el-table-column
            sortable
            prop="totals"
            width="140"
            label="面试题目数量"
          >
          </el-table-column>
          <el-table-column align="center" label="状态">
            <template slot-scope="scope">
              <span v-if="scope.row.state == 1">启用</span>
              <span v-else>禁用</span>
            </template>
          </el-table-column>
          <el-table-column fixed="right" prop="name" label="操纵">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                @click="handleStatus(scope.row)"
              >
                <span v-if="scope.row.state == 0">启用</span>
                <span v-else>禁用</span>
              </el-button>
              <el-button
                :disabled="!!scope.row.state"
                type="text"
                @click.native="editSubListMes(scope.row)"
                size="small"
                >修改
              </el-button>
              <el-button
                :disabled="!!scope.row.state"
                type="text"
                @click.native="delSub(scope.row.id)"
                size="small"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination
          style="float: right"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="subQuery.page"
          :page-sizes="[10, 50, 100]"
          :page-size="subQuery.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
        <!-- dialog区域 -->
        <el-dialog title="提示" :visible.sync="dialogVisible" width="40%">
          <subListDialog
            ref="subDate"
            v-if="dialogVisible"
            :row="row"
            :is-edit="isEdit"
            :visible.sync="dialogVisible"
            @close="closeHandler"
          >
          </subListDialog>
        </el-dialog>
      </div>
      <div class="right"></div>
    </el-card>
  </div>
</template>

<script>
import mixs from '@/mixins/pageMixin.js'
import moment from 'moment'
import { list, remove } from '@/api/hmmm/directorys.js'
import subListDialog from './subListDialog.vue'
export default {
  name: 'directorys',
  mixins: [mixs],
  components: {
    subListDialog
  },
  data () {
    return {
      row: {},
      total: 1,
      dialogVisible: false,
      subjectDialog: false,
      isEdit: false,
      subjectName: {},
      subQuery: {
        page: 1,
        pagesize: 10
      },
      form: {
        name: '',
        subjectName: ''
        // directoryName: ''
      },
      state: '',
      directoryName: '',
      tableData: []
    }
  },
  created () {
    if (this.$route.query.name) {
      this.subQuery.subjectID = this.$route.query.id
      console.log(this.$route.query.id)
      // this.subQuery.directoryName = this.$route.query.name
    }
    this.getSubList()
  },
  methods: {
    async getSubList () {
      const { data: res } = await list(this.subQuery)
      this.tableData = res.items
      this.total = res.counts
    },
    // 删除
    async delSub (data) {
      this.$confirm('此操作将删除学科 ' + ', 继续?', '提示', {
        type: 'warning'
      })
        .then(async () => {
          await remove({ id: data })
            .then((response) => {
              this.$message.success('成功删除了学科' + '!')
              this.tableData.splice(data, 1)
              this.getSubList(this.subQuery)
            })
            .catch((response) => {
              this.$message.error('删除失败!')
            })
        })
        .catch(() => {
          this.$message.info('已取消操作!')
        })
    },
    // 查询
    async querySub () {
      if (this.directoryName !== '') {
        this.subQuery.directoryName = this.directoryName
      }
      if (this.state !== '') {
        this.subQuery.state = this.state
      }
      this.getSubList()
    },
    // 编辑
    editSubListMes (row) {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.subDate.SubjectList = { ...row }
        console.log('gggg', this.$refs.subDate.SubjectList)
      })
      this.isEdit = true
      // this.row = row
    },
    // moment格式化时间
    dateFormat (row, column) {
      const date = row[column.property]
      if (date === undefined) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },

    // 发ajax 渲染主页面表格
    async getSubMes () {
      const { data: res } = await list(this.subQuery)
      this.tableData = res.items
      this.total = res.counts
    },
    clear () {
      this.directoryName = ''
    },
    closeHandler () {
      this.dialogVisible = false
      this.isEdit = false
      this.getSubMes()
    },
    // 序号排序方法
    indexMethods (index) {
      return index + 1 + (this.subQuery.page - 1) * this.subQuery.pagesize
    },
    // 分页数量切换
    handleSizeChange (val) {
      this.subQuery.pagesize = val
      console.log(`每页 ${val} 条`)
      this.getSubMes()
    },
    // 分页跳转
    handleCurrentChange (val) {
      this.subQuery.page = val
      console.log(`当前页: ${val}`)
      this.getSubMes()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-icon-back {
  font-family: element-icons !important;
  speak: none;
  font-style: normal;
  font-weight: 400;
  font-variant: normal;
  text-transform: none;
  line-height: 1;
  vertical-align: baseline;
  display: inline-block;
  -webkit-font-smoothing: antialiased;
}

.container {
  padding: 20px 30px;
}
.page-tools {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .tips {
    line-height: 34px;
    padding: 0px 15px;
    border-radius: 5px;
    border: 1px solid rgba(145, 213, 255, 1);
    background: rgba(230, 247, 255, 1);
    i {
      margin-right: 10px;
      color: #409eff;
    }
  }
}
</style>
<style>
#subMangerDesign {
  color: #606266 !important;
}
.el-breadcrumb__inner.is-link {
  color: #606266 !important;
}
</style>
